package com.kuang.controller;

import com.sun.tools.javac.Main;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class LoginController {

    @RequestMapping("/toMain")
    public String toMain(){
        return "main";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session,Model model){
        System.out.println("进入登录Controller");
        if(username.equals("admin") || password.equals("123456")){
            //传过去用户信息
            session.setAttribute("userLogInfo",username);
            System.out.println("登陆成功!");
            model.addAttribute("user",username);
            return "main";
        }

        return "error";
    }

    //登出 核心方法销毁Session
    @RequestMapping("/goOut")
    public String goOut(HttpSession session){
        session.removeAttribute("userLogInfo");

        return "main";
    }
}
